<!DOCTYPE html>
<html>

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>json转oc model类</title>
		<style type="text/css">
			body {
				font-size: 85%;
			}
			
			.q {
				border-radius: 6px;
				background-color: #eee;
				margin: 1.6em 0.8em;
				padding: 0.8em;
			}
			
			#main {
				padding: 0.8em;
			}
			
			em {
				font-style: normal;
				color: #39f;
			}
			
			textarea {
				padding: 6px;
			}
			
			#err {
				color: #f33;
			}
		</style>
	</head>

	<body>
		<div id="main">
			<input id="superClassName" placeholder="继承的父类(填空为NSObject)" value="ResponseObject" />
			<input id="cSuperClassName" placeholder="所有子类继承的父类(填空为NSObject)" value="CHQModel" />
			<input id="className" placeholder="Model 类名" />
			<br/> json 字符串:
			<p><textarea id="i" name="" rows="22" cols="44"></textarea><br><span id="err"></span></p>
			<!--oc h 头文件:-->
			<!--<p><textarea id="och" name="" rows="22" cols="33"></textarea></p>-->
			<p><button id="g" type="button">Json To Model</button></p>

		</div>
		<pre id="jsf" style="display:none;">0</pre>
		<script type="text/javascript" src="jquery-1.9.1/jquery.min.js"></script>
		<script type="text/javascript" src="js/FileSaver.min.js"></script>
		<script type="text/javascript" src="js/tableExport.js"></script>
		<script type="text/javascript" src="js/createOCH.js"></script>
		<script>
			document.getElementById('g').addEventListener('click', function(evt) {
				var sT = document.getElementById('i').value;
				var oJ = null;
				var hBody = "";
				var mBody = "";
				try {
					oJ = JSON.parse(sT);

					var sJ = JSON.stringify(oJ, 0, 4);

					openJSON(sJ);

					var className = document.getElementById("className").value
					var superClass = document.getElementById("superClassName").value
					var sSuperClass = document.getElementById("cSuperClassName").value

					createTheOCModelFile(oJ, className, superClass, sSuperClass);

					if(className == "") {
						alert("请输入Model类名");
						return;
					}
					var hfile = new File(createOCH.hBody, className + ".h", {
						type: "text/plain;charset=utf-8"
					});
					saveAs(hfile);

					var mfile = new File(createOCH.mBody, className + ".m", {
						type: "text/plain;charset=utf-8"
					});
					saveAs(mfile);

				} catch(e) {
					document.getElementById('err').innerHTML = e;
				}
			});

			function openJSON(sJ) {
				document.getElementById('i').value = sJ;
				//document.getElementById('jsf').innerText = sJ;
				var eF = document.createElement('div');
				sJ.split('\n').map(function(s) {
					var oM = s.match(/\s+".+[^\\]":/);
					var aS = [s];
					if(oM) {
						aS = [s.slice(0, oM[0].length), s.slice(oM[0].length)]
					}
					var eTxt = document.createTextNode(aS[0]);
					if(aS.length > 1) {
						var eEm = document.createElement('em');
						var aK = aS[0].split('"');
						var s1 = aK.shift();
						var s3 = aK.pop();
						//							s3 = aK.pop() + s3;
						eEm.innerText = aK.join('');
						eF.appendChild(document.createTextNode(s1 + '"'));
						eF.appendChild(eEm);
						eF.appendChild(document.createTextNode('"' + s3));
						var eTxt = document.createTextNode(aS[1]);
					}
					eF.appendChild(eTxt);
					eF.appendChild(document.createElement('br'));
				});
				document.getElementById('jsf').removeChild(document.getElementById('jsf').childNodes[0]);
				document.getElementById('jsf').appendChild(eF);
				document.getElementById('main').style.float = 'right';
				document.getElementById('jsf').style.display = 'block';

			}
		</script>
	</body>

</html>